/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

package com.bah.tract.service;

import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.xml.ws.WebServiceContext;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.wsf.spi.annotation.TransportGuarantee;
import org.jboss.wsf.spi.annotation.WebContext;

import com.bah.tract.mapping.WorkStoreFactory;

@javax.jws.WebService(serviceName = "ProjectService", portName = "ProjectService", targetNamespace = "http://bah.com/tract/service", wsdlLocation = "META-INF/project-service.wsdl", endpointInterface = "com.bah.tract.service.ProjectService")
@Stateless
@WebContext(contextRoot = "tract/services", urlPattern = "/ProjectService", transportGuarantee = TransportGuarantee.CONFIDENTIAL)
@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class ProjectServiceBean implements ProjectService {

	private static final Log LOG = LogFactory.getLog(ProjectServiceBean.class
			.getName());

	@Resource
	private WebServiceContext context;

	@PersistenceContext
	private EntityManager em;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bah.tract.service.ProjectService#getAllProjects(*
	 */
	@SuppressWarnings("unchecked")
	public com.bah.tract.service.ProjectList getAllProjects()
			throws TractServiceException_Exception {
		com.bah.tract.model.Person user = AuthenticationHelper.getUser(context,
				em);
		LOG.info(user.getBoozAllenID() + " called getAllProjects.");
		Query query = em.createQuery("Select p from Project p");
		ProjectList _return = new ProjectList();
		List<Project> projects = _return.getProject();
		for (com.bah.tract.model.Project project : (Collection<com.bah.tract.model.Project>) query
				.getResultList()) {
			Project sProject = new Project();
			try {
				BeanUtils.copyProperties(sProject, project);
			} catch (IllegalAccessException e) {
				throw new TractServiceException_Exception(
						"Failed to retrieve Projects", e);
			} catch (InvocationTargetException e) {
				throw new TractServiceException_Exception(
						"Failed to retrieve Projects", e);
			}
			projects.add(sProject);
		}
		return _return;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bah.tract.service.ProjectService#getProjectStores(long project
	 * )*
	 */
	public com.bah.tract.service.WorkStoreList getProjectStores(long projectId)
			throws TractServiceException_Exception {
		com.bah.tract.model.Person user = AuthenticationHelper.getUser(context,
				em);
		LOG.info(user.getBoozAllenID()
				+ " called getProjectStores for project " + projectId);
		com.bah.tract.model.Project project = em.find(
				com.bah.tract.model.Project.class, projectId);
		if (project == null) {
			throw new TractServiceException_Exception("No project with id "
					+ projectId + " found.");
		}
		WorkStoreList _return = new WorkStoreList();
		List<com.bah.tract.model.WorkStore> workStores = project
				.getWorkStores();
		for (com.bah.tract.model.WorkStore workStore : workStores) {
			_return.getWorkStore()
					.add(WorkStoreFactory.getWorkStore(workStore));
		}
		return _return;
	}

}
